package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.contract.utils.ScreenUtil;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;

import java.util.Collections;
import java.util.List;

public class ChannelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private int resID;
    private Context mContext;
    private List<PermissionMenuEntity.ListEntity> mSortedList;
    public boolean isEdit;//是否是编辑状态
    private RovedClicklistensr rovedClicklistensr;
    private int type;
    public ChannelAdapter(Context mContext, List<PermissionMenuEntity.ListEntity> list) {
        this.mContext = mContext;
        mSortedList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_my_manger_more, parent, false);
        return new ChannelHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        setChannel((ChannelHolder) holder, mSortedList.get(position));
    }


    private void setChannel(final ChannelHolder holder, final PermissionMenuEntity.ListEntity bean) {
        holder.name.setText(bean.getTitle());
        int position = holder.getLayoutPosition();
        //点击标签删除已选标签
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    if (isEdit) {
                        removeFromSelected(holder);
                    }
            }
        });
        //长按进入编辑状态
//        holder.allimg.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                isEdit = true;
//                notifyDataSetChanged();
//                //返回true 防止长按拖拽事件跟点击事件冲突
//                return true;
//            }
//        });
        //点击X删除已选标签
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeFromSelected(holder);
            }
        });
        holder.allimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rovedClicklistensr.onItemClicklinener(mSortedList.get(position));
            }
        });
        //是否是编辑状态且第一个不可编辑
        if (isEdit) {
//            if (type==1){
//                if (bean.getTitle().equals("邀请会员")||bean.getTitle().equals("申请会员")||bean.getTitle().equals("员工加入")){
//                    holder.delete.setVisibility(View.GONE);
//                }else {
//                    holder.delete.setVisibility(View.VISIBLE);
//                }
//            }else {
                holder.delete.setVisibility(View.VISIBLE);
//            }

        } else {
            holder.delete.setVisibility(View.GONE);
        }
        //获取entity的图标信息
        String icon = bean.getIcon();
        if ("4_wodebaojia".equals(bean.getName())) {
            icon = "wodebaojia_4";
        }

        if ("dizhiguanli".equals(bean.getIcon()) && ("18_dizhiguanli".equals(bean.getName()) || "0_dizhiguanli".equals(bean.getName()))){
            icon = "dizhiguanli_2";
        }

        if ("0_shangpincuxiao".equals(bean.getName()) && "shangpincuxiao".equals(bean.getIcon())) {
            icon = "shangpincuxiao_2";
        }

        if ("2_wodebaojia".equals(bean.getName())) {
            icon = "wodebaojia_2";
        }

        if ("8_wodebaojia".equals(bean.getName())) {
            icon = "wodebaojia_8";
        }

        if ("0_tuanduixinxi".equals(bean.getName())) {
            icon = "jibenxinxi";
        }

        if ("7_jingjiafabu".equals(bean.getName()) && "jingjiafabu".equals(bean.getIcon())) {
            icon = "jingjiafabuwuliu";
        }

        if ("15_zhishichanquan".equals(bean.getName())) {
            icon = "zhishichanquan_15";
        }
        if ("5_hanxun".equals(bean.getName())){
            icon = "hanxun";
        }
        if ("10_xunjia".equals(bean.getName()) || "xunjia".equals(bean.getIcon())){
            icon = "xunjia_2";
        }
        if ("12_ucjingjia".equals(bean.getName()) || "ucjingjia".equals(bean.getIcon())){
            icon = "jingjia_2";
        }
        if ("15_qiyezhuanxiang".equals(bean.getName()) || "qiyezhuanxiang1".equals(bean.getIcon())){
            icon = "qiyezhuanxiang_2";
        }
        //判断是否存在
        if (!TextUtils.isEmpty(icon)) {
            //获取到对应的本地图标地址id
            resID = mContext.getResources().getIdentifier(icon, "drawable", mContext.getPackageName());
            Log.e("图标名称", icon);
        }
        //判断本地图标地址id是否找到
        if (resID != 0) {
            Glide.with(mContext)
                    .load(resID)
                    .apply(new RequestOptions()
                            .error(R.drawable.user_center_def_img)
                            .placeholder(R.drawable.user_center_def_img)
                            .fallback(R.drawable.user_center_def_img))
                    .into(holder.img);
        } else {
            Glide.with(mContext).load(R.drawable.user_center_def_img).into(holder.img);
        }

    }
    public void setisEdit(boolean isEdit){
        this.isEdit =isEdit;
        notifyDataSetChanged();
    }
    /**
     * 删除已选标签
     * @param holder
     */
    private void removeFromSelected(ChannelHolder holder) {
        int position = holder.getLayoutPosition();
        rovedClicklistensr.onRovedClicklistener(mSortedList.get(position));
        holder.delete.setVisibility(View.GONE);
        notifyItemRemoved(position);
        mSortedList.remove(position);
        notifyDataSetChanged();

    }

    /**
     * 对拖拽的元素进行排序
     * @param fromPosition
     * @param toPosition
     */
    void itemMove(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(mSortedList, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(mSortedList, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public int getItemCount() {
        return mSortedList == null ? 0 : mSortedList.size();
    }

    class ChannelHolder extends RecyclerView.ViewHolder {
        TextView name;
        ImageView delete;
        LinearLayout allimg;
        ImageView img;
        public ChannelHolder(View itemView) {
            super(itemView);
            allimg =itemView.findViewById(R.id.allimg);
            delete = (ImageView) itemView.findViewById(R.id.remove);
            img = (ImageView) itemView.findViewById(R.id.img);
            name =itemView.findViewById(R.id.moretitle);
        }
    }
    public void AddList(PermissionMenuEntity.ListEntity permissionMenuEntity){
         mSortedList.add(permissionMenuEntity);
         notifyDataSetChanged();
    }
    public  List<PermissionMenuEntity.ListEntity> getlist(){
            return mSortedList;
    }

    public void setonRovedClicklistener(RovedClicklistensr rovedClicklistensr){
         this.rovedClicklistensr =rovedClicklistensr;
    }

    public interface RovedClicklistensr{
        void onRovedClicklistener(PermissionMenuEntity.ListEntity listEntity);
        void onItemClicklinener(PermissionMenuEntity.ListEntity listEntity);
    }
    public void settype(int type){
         this.type =type;
    }
}
